/*
 * Reflexis is Copyright (C) 2007, by it-raum.ch
 * martin.imobersteg@it-raum.ch
 *
 * It is free software; you can redistribute it and/or modify it under the terms of either:
 * a) the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version, or
 * b) the "Artistic License".
 */

#include "Reflexis.h"
#include "ProbeReader.h"
#include "ProbeWriter.h"
#include "Probe.h"
#include "SettingsDialog.h"

#include <QtGui>

Reflexis::Reflexis(QWidget *parent) : QWidget(parent)
{
	setupUi(this);
	setWindowTitle(tr("Reflexis - New Probe"));
	createTrayIcon();

	m_reader = new ProbeReader(this);
	m_writer = new ProbeWriter(this);
	m_settings = new SettingsDialog();

	m_writer->connect();

	connect(m_reader, SIGNAL(probeReady(Probe)), this, SLOT(probeReady(Probe)));
	connect(m_cancelButton, SIGNAL(clicked()), this, SLOT(cancelProbe()));
	connect(m_saveButton, SIGNAL(clicked()), this, SLOT(writeProbe()));
}

Reflexis::~Reflexis()
{
	delete m_reader;
	delete m_writer;
}

void Reflexis::createTrayIcon()
{
	m_quitAction = new QAction(tr("&Quit"), this);
	m_quitAction->setIcon(QIcon(":/img/dialog-error.svg"));
	connect(m_quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));

	m_settingsAction = new QAction(tr("&Settings"), this);
	m_settingsAction->setIcon(QIcon(":/img/preferences-system.svg"));
	connect(m_settingsAction, SIGNAL(triggered()), this, SLOT(showSettings()));

	m_connectAction = new QAction(tr("&Connect"), this);
	m_connectAction->setIcon(QIcon(":/img/network-wireless.svg"));
	connect(m_connectAction, SIGNAL(triggered()), this, SLOT(showSettings()));

	m_debugAction = new QAction(tr("&Debug"), this);
	connect(m_debugAction, SIGNAL(triggered()), this, SLOT(debug()));

	m_trayIconMenu = new QMenu(this);
	//m_trayIconMenu->addAction(m_debugAction);
	//m_trayIconMenu->addAction(m_connectAction);
	//m_trayIconMenu->addSeparator();
	m_trayIconMenu->addAction(m_settingsAction);
	m_trayIconMenu->addSeparator();
	m_trayIconMenu->addAction(m_quitAction);

	m_trayIcon = new QSystemTrayIcon(this);
	m_trayIcon->setContextMenu(m_trayIconMenu);
	m_trayIcon->setIcon(QIcon(":/img/reflexis.svg"));
	m_trayIcon->show();
}

void Reflexis::setVisible(bool visible)
{
	QWidget::setVisible(visible);
}

void Reflexis::closeEvent(QCloseEvent *event)
{
	event->ignore();
}

void Reflexis::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
	switch (reason)
	{
		case QSystemTrayIcon::Trigger:
		case QSystemTrayIcon::DoubleClick:
			break;
		case QSystemTrayIcon::MiddleClick:
			break;
		default:
			;
	}
}

void Reflexis::probeReady(Probe probe)
{
	m_probe = probe;
	
	QString name = m_writer->getClientName(m_probe.getIdent());
	
	if (name != "NOK")
	{
		m_identLine->setText(name);
		m_saveButton->setDisabled(false);
	}
	else
	{
		m_identLine->setText("Can't find client #" + m_probe.getIdent());
		m_saveButton->setDisabled(true);
	}

	m_dateLine->setText(m_probe.getDate());
	m_timeLine->setText(m_probe.getTime());
	m_resultLine->setText(m_probe.getResult());
	m_infoLine->setText(m_probe.getInfo());
	m_textLine->setText(m_probe.getText());

	setVisible(true);
}

void Reflexis::writeProbe()
{
	m_writer->writeProbe(m_probe);
	setVisible(false);
}

void Reflexis::cancelProbe()
{
	setVisible(false);
}

void Reflexis::showSettings()
{
	qDebug("settings");
	m_settings->show();
}

void Reflexis::debug()
{
	qDebug("debug");
	setVisible(true);
}

void Reflexis::about()
{
	QMessageBox::information(this, tr("Reflexis"), tr("&copy; 2007 it-raum.ch"));
}
